package com.sgb.kjwl.view.ui.activity.self.company

import android.content.Context
import android.content.Intent
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivityCentralizedMiningBinding
import com.sgb.kjwl.di.self.DaggerSelfComponent
import com.sgb.kjwl.di.self.SelfModule
import com.sgb.link.entity.PickFilterEntity
import com.sgb.kjwl.view.ui.fragment.manage.OtherInquiryFragment
import com.sgb.kjwl.view.ui.fragment.self.company.AgentCentralizedMiningFragment
import com.sgb.kjwl.view.ui.fragment.self.company.AgentGetCentralizedMiningFragment
import com.swgk.core.base.di.AppComponent

/**
 * 集采列表
 */
class AgentCentralizedMiningActivity : AppActivity(), AgentCentralizedMiningFragment.UpdateNumCallback{

    private lateinit var mBinding : ActivityCentralizedMiningBinding

    private lateinit var mAdapter : AgentCentralizedMiningPageAdapter

    private var mTitles : ArrayList<PickFilterEntity<ArrayList<PickFilterEntity<Any>>>> ?= null

    companion object{
        const val KEY_TITLES = "key_titles"

        fun launch(context: Context){
            val intent  = Intent(context, AgentCentralizedMiningActivity::class.java)
            context.startActivity(intent)
        }

        fun launch(context: Context, titles : ArrayList<PickFilterEntity<ArrayList<PickFilterEntity<Any>>>> = ArrayList()){
            val intent  = Intent(context, AgentCentralizedMiningActivity::class.java)
            intent.putExtra(KEY_TITLES, titles)
            context.startActivity(intent)
        }
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        super.setupActivityComponent(appComponent)
        DaggerSelfComponent.builder().appComponent(appComponent)
            .selfModule(SelfModule()).build().inject(this)
    }

    override fun initView() {
        super.initView()
        if (intent != null){
            mTitles = intent.getSerializableExtra(KEY_TITLES) as ArrayList<PickFilterEntity<ArrayList<PickFilterEntity<Any>>>>?
        }
        if (mTitles == null || mTitles!!.isEmpty()){
            finish()
            return
        }
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_centralized_mining)
        mBinding.include.ivBack.setOnClickListener { finish() }
        mBinding.include.titleView.text = "集采列表"
        initTabLayout(0)
    }

    private fun initTabLayout(currentItem : Int) {
        mAdapter = AgentCentralizedMiningPageAdapter(supportFragmentManager, mTitles!!)
        mBinding.viewPage.adapter = mAdapter
        mBinding.viewPage.offscreenPageLimit = mTitles!!.size
        mBinding.tablayout.setupWithViewPager(mBinding.viewPage)
        mBinding.tablayout.tabMode = TabLayout.MODE_FIXED
        mBinding.viewPage.currentItem = currentItem
    }


    class AgentCentralizedMiningPageAdapter(fm : FragmentManager, titles : ArrayList<PickFilterEntity<ArrayList<PickFilterEntity<Any>>>>) : FragmentPagerAdapter(fm) {
        private var mTitles : ArrayList<PickFilterEntity<ArrayList<PickFilterEntity<Any>>>> ?= null
        init {
            this.mTitles = titles
        }
        override fun getCount(): Int {
            if (mTitles != null){
                return mTitles!!.size
            }
            return 0
        }

        // 动态设置我们标题的方法

        fun setPageTitle(position: Int, title : String){
            if (position >= 0 && position < mTitles!!.size){
                mTitles!![position].name = title
                notifyDataSetChanged()
            }
        }


        override fun getItem(position: Int): Fragment {
            if (mTitles == null) return OtherInquiryFragment.newInstance()
            return AgentGetCentralizedMiningFragment.newInstance(mTitles!![position].children)
        }

        override fun getPageTitle(position: Int): CharSequence {
            if (mTitles != null){
                return mTitles!![position].name
            }
            return ""
        }


    }

    override fun updateNum() {
        if (mTitles!!.isEmpty()) return
        val currentItem = mBinding.viewPage.currentItem
        if (currentItem < mTitles!!.size) {
            mAdapter.setPageTitle(currentItem, getReallyName(mTitles!![currentItem].name))
        }
        refreshFragmentNum()
    }

    private fun refreshFragmentNum(){
        val fragment = supportFragmentManager.findFragmentByTag(
            "android:switcher:" + mBinding.viewPage.id.toString() + ":" + 0)
        if (fragment != null && fragment is AgentGetCentralizedMiningFragment) {
            fragment.updateNum()
        }
    }

    private fun getReallyName(name: String): String {
        val start = name.indexOf("(")
        val end = name.indexOf(")")
        try {
            val reallyName =  name.subSequence(start + 1, end)
            var nameInt = reallyName.toString().toInt()
            return name.subSequence(0, start + 1).toString().plus(nameInt - 1) + name.subSequence(end, name.length)
        } catch (e : Exception){
            return ""
        }

    }
}